home *** CD-ROM | disk | FTP | other *** search
/ Super PC 34 / Super PC 34 (Shareware).iso / spc / UTIL / DJGPP2 / V2 / DJLSR200.ZIP / src / libc / posix / unistd / write.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-01-24  |  1.5 KB  |  73 lines

  1. /* Copyright (C) 1996 DJ Delorie, see COPYING.DJ for details */
  2. /* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
  3. #include <libc/stubs.h>
  4. #include <unistd.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <fcntl.h>
  8. #include <io.h>
  9. #include <errno.h>
  10.  
  11. #include <libc/dosio.h>
  12. #include <libc/bss.h>
  13.  
  14. static char *sbuf = 0;
  15. static size_t sbuflen = 0;
  16.  
  17. static int write_count = -1;
  18.  
  19. ssize_t
  20. write(int handle, const void* buffer, size_t count)
  21. {
  22.   if (count == 0)
  23.     return 0; /* POSIX.1 requires this */
  24.  
  25.   if(__file_handle_modes[handle] & O_BINARY)
  26.     return _write(handle, buffer, count);
  27.   else
  28.   {
  29.     int nput, ocount;
  30.     unsigned bufp=0, sbufp=0, crcnt=0;
  31.     const char *buf;
  32.     ocount = count;
  33.  
  34.     /* Force reinitialization in restarted programs (emacs).  */
  35.     if (write_count != __bss_count)
  36.     {
  37.       write_count = __bss_count;
  38.       sbuf = 0;
  39.       sbuflen = 0;
  40.     }
  41.  
  42.     if(sbuflen < 2*count)
  43.     {
  44.       if(sbuf != 0)
  45.     free(sbuf);
  46.       sbuflen = 2*count;
  47.       sbuf = (char *)malloc(sbuflen);
  48.       if (sbuf == 0)
  49.       {
  50.         errno = ENOMEM;
  51.         return -1;
  52.       }
  53.     }
  54.     buf = buffer;
  55.     while (ocount--)
  56.     {
  57.       if(buf[bufp] == 10)
  58.       {
  59.     crcnt++;
  60.     sbuf[sbufp++] = 13;
  61.       }
  62.       sbuf[sbufp++] = buf[bufp++];
  63.     }
  64.     ocount = count;
  65.     count += crcnt;
  66.     buffer = sbuf;
  67.     nput = _write(handle, buffer, count);
  68.     if (nput < ocount)
  69.       return nput;        /* Maybe disk full? */
  70.     return ocount;        /* But don't return count with CR's (TC) */
  71.   }
  72. }
  73.